//
//  RootViewController.m
//  CBPickerTest
//
//  Created by shuai pan on 2017/11/22.
//  Copyright © 2017年 foreveross. All rights reserved.
//

#import "RootViewController.h"
#import "CBPic2ker.h"
#import "UILabel+HTFloat.h"
#import "BaseNavigationController.h"

@interface RootViewController ()<CBPickerControllerDelegate>
@property (strong, nonatomic) UIImageView *userIcon;
@property (strong, nonatomic) UIButton *selectPicker;
@property (strong, nonatomic) UILabel *userTitle;
@property (strong, nonatomic) UISwitch *switchBtn;
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.userIcon];
    [self.view addSubview:self.userTitle];
    [self.view addSubview:self.selectPicker];
    [self.view addSubview:self.switchBtn];

}
#pragma mark    - Action Method
- (void)selectPicker:(UIButton *)sender {
    CBPhotoSelecterController *controller = [[CBPhotoSelecterController alloc] initWithDelegate:self];
    controller.columnNumber = 4;
    controller.maxSlectedImagesCount = 5;
    
    BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:controller];
    [self presentViewController:nav animated:YES completion:nil];
    
}
- (void)switchBtnClcik:(UISwitch *)switchBtn {
    self.notchOpenStatusBar = switchBtn.on;
}

#pragma mark    - CBPickerControllerDelegate
- (void)photoSelecterController:(CBPhotoSelecterController *)pickerController sourceAsset:(NSArray *)sourceAsset {
    __weak typeof(self) weakSelf = self;
    
    if (sourceAsset.count>0 && [sourceAsset[0] isKindOfClass:[PHAsset class]]) {
        PHAsset *asset = (PHAsset*)sourceAsset[0];
        [[PHImageManager defaultManager] requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
            [weakSelf.userIcon setImage:[UIImage imageWithData:imageData]];
            id url = [info objectForKey:@"PHImageFileURLKey"];
            if ([url isKindOfClass:[NSURL class]]) {
                [self.userTitle heightToFit:((NSURL *)url).relativePath fontSize:14.f];
                [self.userTitle setText:((NSURL *)url).relativePath];
            }
            
        }];
        //        [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(80, 80) contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
        //            [weakSelf.userIcon setImage:result];
        //                        NSLog(@"====== %@",info);
        //
        //        }];
    }
    
    
    
}
- (void)photoSelecterDidCancelWithController:(CBPhotoSelecterController *)pickerController {
    
}



#pragma mark    - UI


- (UIImageView *)userIcon {
    if (!_userIcon) {
        _userIcon = [[UIImageView alloc] initWithFrame:CGRectMake((CGRectGetWidth(self.view.frame)-150)/2, CGRectGetMinY(self.view.frame)+100, 150,150)];
        
    }
    return _userIcon;
}
- (UILabel *)userTitle {
    if (!_userTitle) {
        _userTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.userIcon.frame)+50, CGRectGetWidth(self.view.bounds), 0.f)];
        _userTitle.font = [UIFont systemFontOfSize:14.f];
        _userTitle.numberOfLines = 0;
        _userTitle.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    return _userTitle;
}
- (UIButton*)selectPicker {
    if (!_selectPicker) {
        _selectPicker = [UIButton buttonWithType:UIButtonTypeSystem];
        _selectPicker.backgroundColor = [UIColor orangeColor];
        _selectPicker.frame = CGRectMake(CGRectGetWidth(self.view.frame)/2-50, CGRectGetMaxY(self.view.frame)-200, 100,35);
        [_selectPicker setTitle:NSLocalizedString(@"select picker", nil) forState:UIControlStateNormal];
        [_selectPicker addTarget:self action:@selector(selectPicker:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectPicker;
}
- (UISwitch *)switchBtn {
    if (!_switchBtn) {
        _switchBtn = [[UISwitch alloc] initWithFrame:CGRectMake((CGRectGetWidth(self.view.frame)-51)/2, CGRectGetMaxY(self.view.frame)-100, 0,0)];
        [_switchBtn addTarget:self action:@selector(switchBtnClcik:) forControlEvents:UIControlEventValueChanged];
    }
    return _switchBtn;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
